# Set eval=TRUE if you want this to run!
install.packages("data.table")
install.packages("ggplot2")
library(data.table)
library(ggplot2)


mpg <- unique(as.data.table(mpg))


2 Recap of the first data viz class

2.1 Smple framework

  • ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
    • map your data columns to parts of the plot in aes
  • ggplot references


2.2 The full ggplot framework

ggplot(data = <DATA>) + 
  <GEOM_FUNCTION>(
     mapping = aes(<MAPPINGS>),
     stat = <STAT>, 
     position = <POSITION>
  ) +
  <COORDINATE_FUNCTION> +
  <FACET_FUNCTION>


2.2.1 Using an example:

ggplot(data = ggplot2::diamonds) +
    geom_bar(
        mapping = aes(x = cut, fill = cut),
        stat = "count",  # this is the default, not necessary in reality
        position = position_identity()  # not necessary in reality
    ) +
    coord_cartesian() +  # not necessary in reality
    facet_grid() # not necessary in reality


2.2.2 Dissecting the above example

ggplot-framework-1 ggplot-framework-2 ggplot-framework-3

source



3 Missed last class

3.1 Same thing multiple ways

Examples:

  • ggtitle(...) vs. labs(title = ...)
  • providing the data in ggplot(data = ...) or geom_*(data = ...)
  • using ggplot() + geom_bar(stat = 'count') or ggplot() + stat_count()
  • and many others…

This can make learning harder and cause frustration. My advice is that you try to learn one way and stick to it.


3.2 Themes

ggplot(data = mpg) +
    geom_point(mapping = aes(x = cty, y = hwy))

ggplot(data = mpg) +
    geom_point(mapping = aes(x = cty, y = hwy)) +
    theme_minimal()

ggplot(data = mpg) +
    geom_point(mapping = aes(x = cty, y = hwy)) +
    theme_dark()

ggplot(data = mpg) +
    geom_point(mapping = aes(x = cty, y = hwy)) +
    theme_light()

ggplot(data = mpg) +
    geom_point(mapping = aes(x = cty, y = hwy)) +
    theme_gray()


3.3 Managing colors when plotting factors

# mpg <- as.data.table(unique(ggplot2::mpg))

mpg[, class := factor(class, levels = c("midsize", "compact", "suv", "2seater", "minivan", 
"pickup", "subcompact"))]

ggplot(data = mpg) +
    geom_bar(mapping = aes(x = class, fill = class))

ggplot(data = mpg) +
    geom_bar(mapping = aes(x = class, fill = class)) +
    scale_fill_manual(
        values = c("blue", "green", "red", "yellow", "black", "magenta", "white"),
        labels = 1:7        
    )

ggplot(data = mpg) +
    geom_bar(mapping = aes(x = class, fill = class)) +
    scale_fill_brewer(palette = "YlOrRd")

Find color palettes here for example.


3.4 Multiple graphs on the same plot

install.packages("patchwork")
## 
## The downloaded binary packages are in
##  /var/folders/21/6klmfhg17dx9d__zjjpvsn540000gn/T//RtmpTCOh2u/downloaded_packages
library(patchwork)
p1 <- ggplot(mpg) + geom_bar(aes(x = class, fill = class)) + scale_fill_brewer(palette = "YlOrRd") + coord_flip()
p2 <- ggplot(mpg) + geom_bar(aes(x = class, fill = class)) + scale_fill_brewer(palette = "Blues") + coord_flip()

p1 + p2

p3 <- ggplot(mpg) + geom_bar(aes(x = class, fill = class)) + scale_fill_brewer(palette = "Accent")

(p1 | p2) /
 p3 + plot_annotation(title = 'Three plots, one title. Wow.')

Find out more about the package here!


3.5 Returning layers in a function

Instead of this:

ggplot(mpg) +
    geom_bar(aes(x = class, fill = class)) +
    scale_color_brewer(palette = 2) +
    ggtitle("Nice plot dude!") +
    coord_cartesian(ylim = c(0, 30))


You can do this:

setPlotAesthetics <- function(palette, title, ylimmax) {
    list(scale_color_brewer(palette = palette),
         ggtitle(title),
         coord_cartesian(ylim = c(0, ylimmax))
    )
}

ggplot(mpg) +
    geom_bar(aes(x = class, fill = class)) +
    setPlotAesthetics(3, "Not bad either!", 120)


3.6 The data behind the plots

p <- ggplot(mpg, aes(hwy)) +
      geom_histogram()

pg <- ggplot_build(p)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
head(pg$data[[1]])
##    y count        x     xmin     xmax     density     ncount   ndensity
## 1  5     5 12.13793 11.58621 12.68966 0.020138889 0.10869565 0.10869565
## 2  0     0 13.24138 12.68966 13.79310 0.000000000 0.00000000 0.00000000
## 3  2     2 14.34483 13.79310 14.89655 0.008055556 0.04347826 0.04347826
## 4 16    16 15.44828 14.89655 16.00000 0.064444444 0.34782609 0.34782609
## 5 28    28 16.55172 16.00000 17.10345 0.112777778 0.60869565 0.60869565
## 6 10    10 17.65517 17.10345 18.20690 0.040277778 0.21739130 0.21739130
##   flipped_aes PANEL group ymin ymax colour   fill size linetype alpha
## 1       FALSE     1    -1    0    5     NA grey35  0.5        1    NA
## 2       FALSE     1    -1    0    0     NA grey35  0.5        1    NA
## 3       FALSE     1    -1    0    2     NA grey35  0.5        1    NA
## 4       FALSE     1    -1    0   16     NA grey35  0.5        1    NA
## 5       FALSE     1    -1    0   28     NA grey35  0.5        1    NA
## 6       FALSE     1    -1    0   10     NA grey35  0.5        1    NA


4 Presentation on Sensible data visualization



5 What we didn’t cover

5.1 Output types


install.packages("plotly")
## 
## The downloaded binary packages are in
##  /var/folders/21/6klmfhg17dx9d__zjjpvsn540000gn/T//RtmpTCOh2u/downloaded_packages
library(plotly)
ggplotly(
    ggplot(mpg) +
        geom_bar(aes(x = class, fill = class))
)


5.2 Extending ggplot

Check out this open source list of extensions!

5.3 And a lot more :)

For inspiration browse the (R graph gallery)[https://www.r-graph-gallery.com/]!



6 Homework


- First person's first plot
- Written feedback of the above plot from the second person
- Amended plot by the second person

- Second person's first plot
- Written feedback of the above plot from the first person
- Amended plot by the first person


Find the rmarkdown template in the shared folder!